from flask import Flask,render_template,request,redirect
app=Flask(__name__)
@app.route("/")
def index():
    return render_template("index.html")
@app.route("/login",methods=['POST','GET'])
def login():
    username = request.form['username']#接受输入的用户名
    userpwd = request.form['userpwd']#接受输入的密码
    if username == 'kongweiqi':
        if userpwd == '21106110':
            return redirect("scrawl")#跳转爬虫路由
        else:
            return redirect("/")
    else:
        return redirect("/")
@app.route("/scrawl")
def xiangce():
    return render_template("scrawl.html")
@app.route("/start",methods=['POST','GET'])
def start():
    name = request.form['targetname']
    url = request.form['targeturl']
    return str(crawl(url,selector="p"))
import requests
from bs4 import BeautifulSoup
def crawl(url,selector):
    headers={        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15"}
    #获取源代码
    htmlcodes = requests.get(url,headers=headers).text
    #解析源代码
    bs = BeautifulSoup(htmlcodes,'html.parser')
    #根据选择器返回文本
    return [item.text for item in bs.select(selector=selector)]
if __name__=='__main__':
    app.run()